<?php
/**
 * 大熊统计用户消费情况需求
 */

namespace App\Http\Controllers\Api;

use App\Http\Services\Oss;
use App\Model\KovStatic;
use Illuminate\Http\Request;

class KovController extends BaseController
{
    protected $koveModel;//kov数据模型

    public function __construct()
    {
       $this->koveModel = new KovStatic();
    }
    
    //创建脚本任务
    public function addDownListExcel(Request $request){
        $type = $request->get('type',0); //生成类型1=全家总动员 2=kov 3=礼物销量 4=送礼详情 5=玩法汇总数据
        $user_ids = $request->get('user_id',0);//用户id
        $start_time = $request->get('date_st','');//开始时间
        $end_time = $request->get('date_ed','');//结束时间
        $excelUrl = $request->get('excelUrl',''); //Excel路径
        $goodsIds = $request->get('goods_id',0);//礼物id
        $isBag = $request->get('is_bag',0);//是否背包礼物1=购买类型 2=背包
        $sendUserIds = $request->get('from_user_ids',0);//送礼人id
        $toUserids = $request->get('to_user_ids',0);//收礼人id
        if(empty($type)){
            return $this->toJson(self::PARAM_ERROR);
        }
        if($type == 1){
            $param = [
                'type' => $type,
                'user_ids' => $user_ids,
                'start_time' => $start_time,
                'end_time' => $end_time,
            ];
        }elseif($type == 2){
            $userIdArr = [];
            if(!empty($excelUrl)){
                //先将Excel读取入库后分页查询
                $excelUrl = ltrim($excelUrl,'/');
                //下载文件
                $filepath = storage_path().'/'.'userlist.xls';
                $oss = new Oss();
                $content = $oss->getFileContent($excelUrl);
                if(empty($content)){
                    return $this->toJson(self::ERROR,[],'文件读取失败');
                }
                file_put_contents($filepath, $content);
                //读取Excel信息
                $array = excel_to_array($filepath, 1, '', 1);
    
                if (!empty($array['content'])) {
                    foreach ($array['content'] as $k => $v) {
                        $v=array_filter($v);
                        $userids[] = $v;
                    }
                    $userIdArr = array_reduce($userids,'array_merge',[]);
                }
            }
            $param = [
                'type' => $type,
                'user_ids' => implode(',',$userIdArr),
            ];
        }elseif($type == 3){
            if(empty($start_time) || empty($end_time)){
                return $this->toJson(self::PARAM_ERROR, [],'时间必传');
            }
            //查询条件是否超出上限
            $count = $this->koveModel->checkoutCount($start_time,$end_time,$goodsIds,$isBag);
            if($count > 20000){
                return $this->toJson(self::ERROR, [],'调整搜索条件,超出上限');
            }
            $param = [
                'type' => $type,
                'user_ids' => 1,
                'gift_ids' => $goodsIds,
                'is_bag' => $isBag,
                'start_time' => $start_time,
                'end_time' => $end_time,
            ];
        }elseif($type == 4){
            if(empty($start_time) || empty($end_time)){
                return $this->toJson(self::PARAM_ERROR, [],'时间必传');
            }
            //查询条件是否超出上限
            $count = $this->koveModel->checkoutCount($start_time,$end_time,$goodsIds,0,$toUserids, $sendUserIds);
            if($count > 20000){
                return $this->toJson(self::ERROR, [],'调整搜索条件,超出上限');
            }
            $param = [
                'type' => $type,
                'user_ids' => 1,
                'from_user_ids' => $sendUserIds,
                'to_user_ids' => $toUserids,
                'gift_ids' => $goodsIds,
                'start_time' => $start_time,
                'end_time' => $end_time,
            ];
        }elseif($type == 5){
            $param = [
                'type' => $type,
                'user_ids' => $user_ids,
                'start_time' => $start_time,
                'end_time' => $end_time,
            ];
        }

        $status = $this->koveModel->addDownListExcel($param);
        return $this->toJson(self::SUCCESS, $status);
    }

    //下载数据列表
    public function downListExcel(Request $request){
        $type = $request->get('type',0);//下载数据类型
        $limit = $request->get('limit',10);//结束时间
        $page = $request->get('page',1);//当前页面

        $data = $this->koveModel->downListExcel($type, $limit, $page);
        return $this->toJson(self::SUCCESS, $data);
    }
}